

#include "Scene0917.hpp"
#include "CommonFunction.hpp"
#include "constant.h"
#include "Scene0910.hpp"
#include "CardGame.hpp"

Scene *Scene0917::scene(){
    return Scene0917::create();
}

bool Scene0917::init(){
    if (!Scene::init()) {
        return false;
    }
    
    int cards[54];
    for (int i = 0; i < 54; i++){
        cards[i] = i;
    }
    CardGame *game = new CardGame();
    game->shuffle(cards, 54);
    
    
    
    
    
    
    Sprite *background = Sprite::create("res/Background.png");
    background->setPosition(Vec2(VISIBLE_SIZE.width/2, VISIBLE_SIZE.height/2));
    addChild(background);
    
    // 自己
    player_self = new Player();
    player_self->account = "科职";
    player_self->position = 3;
    loadPlayerUI(player_self);
    
    // 菜单
    MenuItemFont *leftItem = MenuItemFont::create("添加左侧玩家", CC_CALLBACK_1(Scene0917::menuItemClicked, this));
    leftItem->setPosition(Vec2(-100, 0));
    leftItem->setTag(1);
    
    MenuItemFont *frontItem = MenuItemFont::create("添加对面玩家", CC_CALLBACK_1(Scene0917::menuItemClicked, this));
    frontItem->setPosition(Vec2(0, 50));
    frontItem->setTag(2);
    
    MenuItemFont *rightItem = MenuItemFont::create("添加右侧玩家", CC_CALLBACK_1(Scene0917::menuItemClicked, this));
    rightItem->setPosition(Vec2(100, 0));
    rightItem->setTag(3);
    
    // 关闭按钮
    MenuItemImage *closeItem = MenuItemImage::create("CloseNormal.png", "CloseSelected.png", CC_CALLBACK_1(Scene0917::menuItemClicked, this));
    closeItem->setTag(100);
    closeItem->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT);
    closeItem->setPosition(Vec2(-VISIBLE_SIZE.width/2, VISIBLE_SIZE.height/2));
    closeItem->setScale(2);
    
    Menu *menu = Menu::create(leftItem, frontItem, rightItem, closeItem, NULL);
    addChild(menu);
    
    
    return true;
}

void Scene0917::setRoomId(int roomId){
    log("房间号： %d", roomId);
    Label *label = Label::createWithSystemFont(StringUtils::format("%s%d", CommonFunction::getChineseWithKey("a")->getCString(), roomId), "Arial", 50);
    label->setAnchorPoint(Vec2::ANCHOR_TOP_RIGHT);
    label->setPosition(Vec2(VISIBLE_SIZE.width, VISIBLE_SIZE.height));
    addChild(label);
}

void Scene0917::menuItemClicked(Ref *sender){
    MenuItemFont *item = (MenuItemFont *)sender;
    switch (item->getTag()) {
        case 1: {// 在左侧添加玩家
            player_left = new Player();
            player_left->account = "左边";
            player_left->position = 2;
            loadPlayerUI(player_left);
            
            item->setString("移除左侧玩家");
            item->setTag(4);
            break;
        }
        case 2: {// 在对面添加玩家
            player_front = new Player();
            player_front->account = "对面";
            player_front->position = 1;
            loadPlayerUI(player_front);
            
            item->setString("移除对面玩家");
            item->setTag(5);
            break;
        }
        case 3: {// 在右侧添加玩家
            player_right = new Player();
            player_right->account = "右边";
            player_right->position = 4;
            loadPlayerUI(player_right);
            
            item->setString("移除右侧玩家");
            item->setTag(6);
            break;
        }
        case 4: {// 从左侧移除玩家
            player_left->node->removeAllChildrenWithCleanup(true);
            
            item->setString("添加左侧玩家");
            item->setTag(1);
            break;
        }
        case 5: {// 从对面移除玩家
            player_front->node->removeAllChildrenWithCleanup(true);
            
            item->setString("添加对面玩家");
            item->setTag(2);
            break;
        }
        case 6: {// 从右侧移除玩家
            player_right->node->removeAllChildrenWithCleanup(true);
            
            item->setString("添加右侧玩家");
            item->setTag(3);
            break;
        }
        default:
            Director::getInstance()->replaceScene(Scene0910::scene());
            break;
    }
}

char Scene0917::getPlayerCharPosition(int pos){
    if (player_self->position == 1) {
        if (pos == 2) return 'r';
        if (pos == 3) return 'f';
        if (pos == 4) return 'l';
    }
    else if (player_self->position == 2) {
        if (pos == 1) return 'l';
        if (pos == 3) return 'r';
        if (pos == 4) return 'f';
    }
    else if (player_self->position == 3) {
        if (pos == 1) return 'f';
        if (pos == 2) return 'l';
        if (pos == 4) return 'r';
    }
    else if (player_self->position == 4) {
        if (pos == 1) return 'r';
        if (pos == 2) return 'f';
        if (pos == 3) return 'l';
    }
    return 's';
}

void Scene0917::loadPlayerUI(Player *player){
    // 把椅子号转换成具体位置
    player->pos_char = getPlayerCharPosition(player->position);
    
    player->createPlayerNode();
    addChild(player->node);
}


